package com.zl.roomdesign.main.dynamic

import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.zl.core.MainApp
import com.zl.core.extend.inflate
import com.zl.core.utils.DateUtils
import com.zl.roomdesign.R
import com.zl.roomdesign.main.ImageEntity
import kotlinx.android.synthetic.main.item_composition.view.*

/**
 *
 *<p></p>
 *
 * Created by zhangli on 2018/5/7 11:18.<br/>
 */
class CompositionAdapter(private var list: MutableList<ImageEntity>, private var onItemClick: ((ViewHolder) -> Unit)? = null) : RecyclerView.Adapter<CompositionAdapter.ViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = parent.inflate(R.layout.item_composition, parent, false)
        return ViewHolder(view)
    }

    override fun getItemCount() = list.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val entity = list[position]

        val url = MainApp.instance.getServerHost() + entity.fileName
        Log.i("url", "onBindViewHolder: $url")
        Glide.with(holder.itemView.compositionImg)
                .load(url)
                .into(holder.itemView.compositionImg)

        holder.itemView.nameTxt.text = entity.userName
        if (entity.uploadTime != null) {
            holder.itemView.timeTxt.text = DateUtils.timeMillisToDateString(entity.uploadTime * 1000, "yyyy-MM-dd HH:mm:ss")
        } else {
            holder.itemView.timeTxt.text = ""
        }
        holder.itemView.setOnClickListener {
            onItemClick?.invoke(holder)
        }
    }


    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}